package com.atguigu.controller;

import com.atguigu.entity.UserFollow;
import com.atguigu.entity.UserInfo;
import com.atguigu.result.Result;
import com.atguigu.service.UserFollowService;
import com.atguigu.vo.UserFollowVo;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/userFollow")
public class UserFollowController {

    @DubboReference
    private UserFollowService userFollowService;

    @RequestMapping("/auth/follow/{houseId}")
    public Result follow(@PathVariable("houseId") Long houseId, HttpServletRequest request){
        HttpSession session = request.getSession();
        UserInfo userInfo= (UserInfo)session.getAttribute("USER");
        userFollowService.follow(userInfo.getId(),houseId);
        return Result.ok();
    }
    /*userFollow/auth/list/'+pageNum+'/'+this.page.pageSize*/
    @RequestMapping("/auth/list/{pageNum}/{pageSize}")
    public Result list(
            @PathVariable("pageNum") Integer  pageNum,
            @PathVariable("pageSize") Integer pageSize,
            HttpServletRequest request
    ){
        UserInfo userInfo =(UserInfo) request.getSession().getAttribute("USER");
        // 获取UserID
        Long userInfoId = userInfo.getId();
        // 以分页的形式返回当前用户关注的房源信息
        PageInfo<UserFollowVo> pageInfo=userFollowService.findListPage(pageNum,pageSize,userInfoId);
        return Result.ok(pageInfo);
    }


    /*auth/cancelFollow/'+id*/
    @RequestMapping("/auth/cancelFollow/{id}")
    @ResponseBody
    public Result cancelFollow(@PathVariable("id") Long id){
        userFollowService.delete(id);
        return Result.ok();
    }

}
